package org.sql.drive;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.init.init;

import util.print.printLogCat;

public class mysqlDAO implements jdbcDAO {
	private String url="jdbc:mysql://localhost:3306/neworder";
	private String user="root";
	private String pass="123456";
	private Connection conn = null;
	private Statement stmt=null;
	private ResultSet rs=null;
	@Override
	public boolean Connection(String url, String user, String pass) {
		this.setURL(url);
		this.setUser(user);
		this.setPass(pass);
		return this.Connection();
	}
	
	public boolean Connection() {
		// TODO Auto-generated method stub
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(init.getInstance().getSqlUrl("mysql")+"?user="+
					init.getInstance().getSqlUser("mysql")+"&password="+
					init.getInstance().getSqlPass("mysql")+
					"&useUnicode=true&characterEncoding=UTF-8");
			
			return true;
		} catch (ClassNotFoundException e) {
			printLogCat.printlnERROR(e.getMessage());
			e.printStackTrace();
			return false;
		} catch (SQLException e) {
			printLogCat.printlnERROR(e.getMessage());
			e.printStackTrace();
			return false;
		}
		
	}

	
	public java.sql.Connection getConn() {
		// TODO Auto-generated method stub
		if(conn==null){
		Connection();
		}
		return conn;
	}

	
	public Statement createStmt() {
		// TODO Auto-generated method stub
		
		try {
			stmt = conn.createStatement();
			
		} catch (SQLException e) {
			e.printStackTrace();
			
		}
		return stmt;
	}

	
	public ResultSet executeQuery(String sql) {
		// TODO Auto-generated method stub
		printLogCat.printlnWriter(sql);
		try {
			if(stmt==null)
				stmt=createStmt();
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			printLogCat.printlnERROR(e.getMessage());
			e.printStackTrace();
		}finally{
			//closeDAO(rs);
			//closeDAO(stmt);
			//closeDAO(conn);
		}
		return rs;
	}

	
	public int executeUpdate(String sql) {
		// TODO Auto-generated method stub
		printLogCat.printlnWriter(sql);
		int ret = 0;
		try {
			if(stmt==null)
				stmt=createStmt();
		
			ret = stmt.executeUpdate(sql);
		} catch (SQLException e) {
			printLogCat.printlnERROR(e.getMessage());
			e.printStackTrace();
		} finally {
			closeDAO(stmt);
		}
		return ret;
	}

	
	public PreparedStatement prepareStmt(String sql) {
		// TODO Auto-generated method stub
		printLogCat.printlnWriter(sql);
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(sql);
			
		} catch (SQLException e) {
			printLogCat.printlnERROR(e.getMessage());
			e.printStackTrace();
		}finally{
			closeDAO(pstmt);
		}
		return pstmt;
	}

	
	public PreparedStatement prepareStmt(String sql, int autoGeneratedKeys) {
		// TODO Auto-generated method stub
		printLogCat.printlnWriter(sql);
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement(sql, autoGeneratedKeys);
			
		} catch (SQLException e) {
			printLogCat.printlnERROR(e.getMessage());
			e.printStackTrace();
		}finally{
			closeDAO(pstmt);
		}
		return pstmt;
	}


	public  void closeRes(ResultSet rs) {
		if(rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				printLogCat.printlnERROR(e.getMessage());
				e.printStackTrace();
			}
			rs = null;
		}
	}


	@Override
	public void setURL(String url) {
		// TODO Auto-generated method stub
		this.url=url;
	}


	@Override
	public void setUser(String user) {
		// TODO Auto-generated method stub
		this.user=user;
		
	}


	@Override
	public void setPass(String pass) {
		// TODO Auto-generated method stub
		this.pass=pass;
	}

	@Override
	public jdbcDAO closeDAO(java.sql.Connection conn) {
		// TODO Auto-generated method stub
		if(conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			conn = null;
		}
		return this;
		
	}

	@Override
	public jdbcDAO closeDAO(ResultSet rs) {
		// TODO Auto-generated method stub
		if(rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rs = null;
		}
		return this;
	
	}

	@Override
	public jdbcDAO closeDAO(Statement stmt) {
		// TODO Auto-generated method stub
		if(stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			stmt = null;
		}
		return this;
	}

	@Override
	public jdbcDAO closeDAOConn() {
		// TODO Auto-generated method stub
		return closeDAO(conn);
	}

	@Override
	public jdbcDAO closeDAORes() {
		// TODO Auto-generated method stub
		return this.closeDAO(rs);
	}

	@Override
	public jdbcDAO closeDAOStmt() {
		// TODO Auto-generated method stub
		return this.closeDAO(stmt);
	}

	@Override
	public jdbcDAO closeDAO() {
		// TODO Auto-generated method stub
		this.closeDAORes();
		this.closeDAOStmt();
		this.closeDAOConn();
		return this;
	}


}
